package com.tianji.aigc.controller;

import com.tianji.aigc.service.ChatSessionService;
import com.tianji.aigc.vo.HistorySessionVO;
import com.tianji.aigc.vo.MessageVO;
import com.tianji.aigc.vo.SessionVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/session")
@RequiredArgsConstructor
public class ChatSessionController {

    private final ChatSessionService chatSessionService;

    /**
     * 创建会话
     *
     * @param num 热门话题数量
     * @return SessionVO 会话信息
     */
    @PostMapping
    public SessionVO createSession(@RequestParam(value = "n", required = false, defaultValue = "3") Integer num) {
        return chatSessionService.createSession(num);
    }

    /**
     * 热门问题
     * @param num 热门话题数量
     * @return List<SessionVO.Example></> 热门问题
     */
    @GetMapping("/hot")
    public List<SessionVO.Example> hotChat(@RequestParam(value = "n", required = false, defaultValue = "3") Integer num){
        return chatSessionService.hotChat(num);
    }

    /**
     * 查询单个历史对话详情
     *
     * @return 对话记录列表
     */
    @GetMapping("/{sessionId}")
    public List<MessageVO> queryBySessionId(@PathVariable("sessionId") String sessionId) {
        return this.chatSessionService.queryBySessionId(sessionId);
    }

    /**
     * 查询历史会话
     *
     * @return 历史对话列表
     */
    @GetMapping("/history")
    public Map<String, List<HistorySessionVO>> queryHistory() {
        return this.chatSessionService.queryHistory();
    }

    /**
     * 修改历史会话的标题
     *
     * @param sessionId 会话id
     * @param title 标题
     */
    @PutMapping("/history")
    public void updateHistory(@RequestParam("sessionId") String sessionId,
                              @RequestParam("title") String title) {
        chatSessionService.updateHistory(sessionId, title);
    }

    /**
     * 删除历史会话
     *
     * @param sessionId 会话id
     */
    @DeleteMapping("/history")
    public void deleteHistory(@RequestParam("sessionId") String sessionId) {
        chatSessionService.deleteHistory(sessionId);
    }

}
